#include "xmiimportdialog.h"

#include "../utils/mimetypemanager.h"
#include "../utils/xmiimporter.h"

#include <coremimetypes.h>
#include <fspersistencecommon.h>
#include <iconmanager.h>
#include <icontainerdocument.h>
#include <istorage.h>
#include <persistence.h>

#include <QtCore/QUrl>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>

using namespace core;
using namespace fspersistence;
using namespace swp;

XmiImportDialog::XmiImportDialog(
    const QSharedPointer<IContainerDocument>& project, QWidget* parent) :
    QDialog(parent),
        m_project(project),
        m_status(0),
        m_import(0),
        m_cancel(0),
        m_inImport(false)
{
    QGridLayout* layout = new QGridLayout(this);

    setWindowTitle(tr("Import from XMI"));

    QLabel* header = new QLabel(tr("<h1>Import XMI document</h1>"));
    QLabel* text = new QLabel(tr("<p>"
        "Please note that only versions <b>2.1</b> and <em>up</em> of XMI "
        "are supported."
        "<p>"
        "Also note that the mapping of elements won't be 100% of the original."
        "</p>"
        "</p>"));
    text->setFixedWidth(350);
    m_status = new QLabel(tr("Import started"));
    m_status->setStyleSheet("QLabel { color: blue; font-weight: bold; }");
    m_status->setVisible(false);

    m_import = new QPushButton(tr("Import"));
    connect(m_import, SIGNAL(clicked()), this, SLOT(import()));

    m_cancel = new QPushButton(tr("Cancel"));
    connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject()));

    layout->addWidget(header, 0, 0, 1, 3);
    layout->addWidget(text, 1, 0, 1, 3);
    layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Minimum,
        QSizePolicy::Minimum), 2, 0);
    layout->addWidget(m_status, 3, 0, 1, 3);
    layout->addItem(new QSpacerItem(10, 10, QSizePolicy::Minimum,
        QSizePolicy::Minimum), 4, 0);
    layout->addWidget(m_import, 5, 0);
    layout->addWidget(m_cancel, 5, 1);
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding,
        QSizePolicy::Minimum), 5, 2);

    layout->setSizeConstraint(QLayout::SetFixedSize);

    setLayout(layout);

    setModal(true);
}

XmiImportDialog::~XmiImportDialog()
{

}

void XmiImportDialog::reject()
{
    if (!m_inImport)
    {
        QDialog::reject();
    }
}

void XmiImportDialog::import()
{
    IStorage* storage = Persistence::getStorage(FILE_SYSTEM_STORAGE);

    if (storage != 0)
    {
        const QUrl url = storage->urlForOpen(
            MimeTypeManager::getInstance()->mimeType(
                XMI_MIMETYPE));

        if (!url.isEmpty())
        {
            m_inImport = true;
            m_import->setEnabled(false);
            m_cancel->setEnabled(false);
            m_status->setVisible(true);
            XmiImporter::getInstance()->import(m_project, url);
            m_status->setText(tr("Import finished"));
            m_inImport = false;
            m_import->setEnabled(true);
            m_cancel->setEnabled(true);
        }
    }
}
